package usercontroller

import (
	"context"
	"gitee.com/go-mid/booter/bootservice"
	"gitee.com/go-mid/booter/web"
	"gitee.com/go-mid/infra/xlog"
	"gitee.com/go-mid/user/userservice"
)

// 查询
type GetUserRequest struct {
	userservice.GetUserReq
} //@name 获取用户表请求

func FactoryGetUserRequest() web.HandleRequest {
	return new(GetUserRequest)
}

//
// @Summary      获取用户表
// @Description  根据id获取用户表详情
// @Tags
// @Param        message	body		GetUserRequest	true	"入参"
// @Success      200  {object}  web.ApiReturn{payload=userservice.GetUserRes} "响应"
// @Router       /user/get [post]
func (m *GetUserRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	fun := "UserController.GetUser -->"
	res, err := userservice.DefaultUserService.GetUser(ctx, &m.GetUserReq)
	if err != nil {
		xlog.Warnf(ctx, "%s DefaultUserService.GetUser err: req: %v  err: %v", fun, m, err)
		return nil, err
	}

	return res, nil
}

// 列表
type ListUserRequest struct {
	userservice.ListUserReq
} //@name 查询用户表请求

func FactoryListUserRequest() web.HandleRequest {
	return new(ListUserRequest)
}

//
// @Summary      查询用户表
// @Description  根据条件查询用户表详情
// @Tags
// @Param        message	body		ListUserRequest	true	"入参"
// @Success      200  {object}  web.ApiReturn{payload=userservice.ListUserRes} "响应"
// @Router       /user/list [post]
func (m *ListUserRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	fun := "UserController.ListUser -->"
	res, err := userservice.DefaultUserService.ListUser(ctx, &m.ListUserReq)
	if err != nil {
		xlog.Warnf(ctx, "%s DefaultUserService.ListUser err: req: %v  err: %v", fun, m, err)
		return nil, err
	}

	return res, nil
}

// 添加
type AddUserRequest struct {
	userservice.AddUserReq
} //@name 添加用户表请求

func FactoryAddUserRequest() web.HandleRequest {
	return new(AddUserRequest)
}

//
// @Summary      添加用户表
// @Description  添加一条用户表记录
// @Tags
// @Param        message	body		AddUserRequest	true	"入参"
// @Success      200  {object}  web.ApiReturn{payload=userservice.AddUserRes} "响应"
// @Router       /user/add [post]
func (m *AddUserRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	fun := "UserController.AddUser -->"
	res, err := userservice.DefaultUserService.AddUser(ctx, &m.AddUserReq)
	if err != nil {
		xlog.Warnf(ctx, "%s DefaultUserService.AddUser err: req: %v  err: %v", fun, m, err)
		return nil, err
	}

	return res, nil
}

// 修改
type UpdateUserRequest struct {
	userservice.UpdateUserReq
} //@name 修改用户表请求

func FactoryUpdateUserRequest() web.HandleRequest {
	return new(UpdateUserRequest)
}

//
// @Summary      修改用户表
// @Description  修改用户表记录
// @Tags
// @Param        message	body		UpdateUserRequest	true	"入参"
// @Success      200  {object}  web.ApiReturn{payload=userservice.UpdateUserRes} "响应"
// @Router       /user/update [post]
func (m *UpdateUserRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	fun := "UserController.UpdateUser -->"
	res, err := userservice.DefaultUserService.UpdateUser(ctx, &m.UpdateUserReq)
	if err != nil {
		xlog.Warnf(ctx, "%s DefaultUserService.UpdateUser err: req: %v  err: %v", fun, m, err)
		return nil, err
	}

	return res, nil
}

// 添加或修改
type UpsertUserRequest struct {
	userservice.UpsertUserReq
} //@name 添加或修改用户表请求

func FactoryUpsertUserRequest() web.HandleRequest {
	return new(UpsertUserRequest)
}

//
// @Summary      添加或修改用户表
// @Description  添加或修改用户表记录
// @Tags
// @Param        message	body		UpsertUserRequest	true	"入参"
// @Success      200  {object}  web.ApiReturn{payload=userservice.UpsertUserRes} "响应"
// @Router       /user/upsert [post]
func (m *UpsertUserRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	fun := "UserController.UpsertUser -->"
	res, err := userservice.DefaultUserService.UpsertUser(ctx, &m.UpsertUserReq)
	if err != nil {
		xlog.Warnf(ctx, "%s DefaultUserService.UpsertUser err: req: %v  err: %v", fun, m, err)
		return nil, err
	}

	return res, nil
}
